package pl.smath.operation.basic;

import java.util.List;
import java.util.Vector;

import pl.smath.expression.SumTerm;
import pl.smath.expression.Term;


/**
 * Zawiera podstawowe operacje no modelu.
 * 
 * @author klangner
 *
 */
public class MultiplyAddOp extends Operation{

	/**
	 * a*(b+c) -> a*b + a*c
	 */
	public Term process(Term t1, SumTerm t2){

		MultiplyOp	multiplyOp = new MultiplyOp();
		List<Term>	terms1;
		List<Term>	terms2 = t2.getTermsList();
		SumTerm outputTerm = new SumTerm();
		
		outputTerm.setNegative(t2.isNegative());
		
		if(t1 instanceof SumTerm){
			terms1 = ((SumTerm)t1).getTermsList();
		}
		else{
			terms1 = new Vector<Term>();
			terms1.add(t1);
		}
		
		
		for(int i = 0; i < terms1.size(); i++){
			
			Term term1 = terms1.get(i);
			for(int j = 0; j < terms2.size(); j++){
				Term term = multiplyOp.forceMultiply(term1, terms2.get(j));
	
				outputTerm.addTerm(term);
			}
		}
		
		return outputTerm;
	}

}
